package co.yixiang.common.security.service;

import co.yixiang.common.core.api.ApiResult;
import co.yixiang.common.core.constant.SecurityConstants;
import co.yixiang.common.core.utils.ServletUtils;
import co.yixiang.retail.common.domain.UserInfo;
import co.yixiang.retail.common.dto.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


import javax.servlet.http.HttpServletRequest;


@Component
public class RetailPhoneUserDetailService extends BaseUserDetailService{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected UserInfo getUser(String phone) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest();
        String loginType = (String) httpServletRequest.getAttribute(SecurityConstants.LOGIN_TYPE);
        logger.info("校验方式"+loginType);
        //1、门店店主  判断信息  是否有该门店
        ApiResult<UserInfo> userInfo  = remoteStoreUserService.getRetailByPhone(phone);

        UserInfo data = userInfo.getData();
        UserDto user =data.getUserDto();
        if(user == null){
            logger.error("找不到该用户，用户账号：" + phone);
            throw new UsernameNotFoundException("找不到该用户，用户账号：" + phone);
        }
        return data;
    }
}
